//
//  SelePushViewController.m
//  UI_Project_A
//
//  Created by GH on 16/3/7.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "SelePushViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "ModelOfVideo.h"
#import <UIImageView+WebCache.h>
#import "PushNewCell.h"
#import "HandleData.h"
#import "ModelOfPushVideo.h"
#import "ModelOfPushNewVideo.h"
#import <DKNightVersion.h>
#import "AVPlaySingle.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT  self.view.frame.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define degreesToRadian(x) (M_PI * (x) / 180.0)

@interface SelePushViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,assign)BOOL isTap;
@property(nonatomic ,assign)BOOL isPlay;
@property(nonatomic ,assign)BOOL isScale;

@property (nonatomic ,retain)UIView *viewOftemp;
@property (nonatomic ,retain)UIView *playerView;
@property (nonatomic ,retain)UIView *bottomView;
@property (nonatomic ,retain)UISlider *ProgressSlider;
@property (nonatomic ,retain)UIButton *playerButton;
@property (nonatomic ,retain)UILabel *timeLabel;

@property(nonatomic,retain)AVPlayer *player;
@property(nonatomic,retain)AVPlayerLayer *playerLayer;
@property(nonatomic,retain)AVPlayerItem *playerItem;
@property (nonatomic ,retain)UIButton *buttonFullscreen;
@property (nonatomic, assign) CGFloat totalMovieDuration; // 保存该视频资源的总时长，快进或快退的时候要用


@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)NSMutableArray *arrayData;
@property (nonatomic ,retain)NSString *url;



@end

@implementation SelePushViewController


- (void) dealloc {
    
    
    [_tableview release];
    [_playerItem release];
    [_playerLayer release];
    [_player release];
    [_timeLabel release];
    [_playerButton release];
    [_ProgressSlider release];
    [_bottomView release];
    [_playerView release];
    [_viewOftemp release];
    [super dealloc];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createtableViewData];
    [self createTableview];

        [[AVPlaySingle playerShare] playerWtihUrl:self.model.mp4_url showInview:self.view];

    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"buttonclick" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {

        [self dismissViewControllerAnimated:YES completion:nil];
    
    }];
    
    
    


}








#pragma  ---------------分割线-------------------
#pragma 下方的tableview


- (void)createtableViewData {
    
    if (self.arrayData == nil) {
        self.arrayData = [NSMutableArray array];
    }
    
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/detail/%@.html",self.model.vid];
    
    [HandleData handleFloewNewWithString:url Block:^(id result) {
                
        NSArray *array = [result objectForKey:@"recommend"];
        
        for (NSDictionary *dic in array) {
            
            ModelOfPushNewVideo *pushModel = [[ModelOfPushNewVideo alloc] init];
            //            [pushModel setValuesForKeysWithDictionary:dic];
            pushModel.title = [dic objectForKey:@"title"];
            pushModel.mp4_url = [dic objectForKey:@"mp4_url"];
            pushModel.cover = [dic objectForKey:@"cover"];
            [self.arrayData addObject:pushModel];
            
            
            [pushModel release];
            
        }
        
        [self.tableview reloadData];
    }];
    
    
    
    
}


- (void)createTableview {
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT /3 +30, WIDTH, HEIGHT - (HEIGHT /3 +30)) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    [self.tableview release];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.tableview registerClass:[PushNewCell class] forCellReuseIdentifier:@"pool"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PushNewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    ModelOfPushNewVideo *model = [self.arrayData objectAtIndex:indexPath.row];
    
    
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell passModelOfPushModel:model];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return 100;
}


#pragma delegate



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfPushVideo *model = [self.arrayData objectAtIndex:indexPath.row];
    
    self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:model.mp4_url]];
    
    [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
    [self.player play];
    
    
    [_playerItem release];
    
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
